package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.*;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/after/agent")
public class AfterAgentController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AgentUserWithdrawService agentUserWithdrawService;
    @Autowired
    AgentUserService agentUserService;
    @Autowired
    AgentUserMessageService agentUserMessageService;
    @Autowired
    AppSsCitiesService appSsCitiesService;
    @Autowired
    AppSsProvincesService appSsProvincesService;
    @Autowired
    private SsAreaService areaService;

    @RequestMapping("/userList")
    public String userList(SplitPageBean splitPageBean, String username) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(username)) {
                paramap.put("username", username);
            }
            List<AgentUser> list = agentUserService.page(splitPageBean, paramap);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/agent/userList");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/gotoAdd")
    public String gotoAdd() {
        return this.sucess("/web/after/agent/add");
    }

    @RequestMapping("/save")
    @ResponseBody
    public Result save(AgentUser param) {
        try {
            AgentUser userByCityId = agentUserService.findUserByAreaId(param.getAreaId());
            if(userByCityId != null) {
                return sucessJSON("当前区域已有代理用户！");
            }
            agentUserService.save(param);

            return sucessJSON("success");
        } catch (Exception e) {
            return errorJSON("系统繁忙");
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(AgentUser param) {
        try {
            AgentUser userByCityId = agentUserService.findUserByAreaIdNotMe(param.getAreaId(), param.getId());
            if(userByCityId != null) {
                return sucessJSON("当前区域已有代理用户！");
            }
            agentUserService.update(param);

            return sucessJSON("success");
        } catch (Exception e) {
            return errorJSON("系统繁忙");
        }
    }

    @RequestMapping("/detail")
    public String detail(Integer id) {
        try {
            AgentUser agentUser = agentUserService.get(id);
            SsArea ssArea = areaService.get(agentUser.getAreaId());
            SsCities citie = appSsCitiesService.getCitie(ssArea.getParentId());
            SsProvinces ssProvinces = appSsProvincesService.get(citie.getProvinceid());
            agentUser.setAreaName(ssArea.getName());
            agentUser.setCityId(citie.getCityid());
            agentUser.setCityName(citie.getCity());
            agentUser.setProvinceId(ssProvinces.getProvinceid());
            agentUser.setProvinceName(ssProvinces.getProvince());

            request.setAttribute("obj", agentUser);
            return this.sucess("/web/after/agent/edit");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }
    }




    @RequestMapping("/withdrawList")
    public String list(SplitPageBean splitPageBean) {
        try {
            List<AgentUserWithdraw> list = agentUserWithdrawService.page(splitPageBean);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/agent/withdrawList");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }
    }

    /**
     * 审核通过
     *
     * @param id
     * @return
     */
    @RequestMapping("/success")
    @ResponseBody
    public Result success(Integer id) {
        agentUserWithdrawService.success(id);

        return this.sucessJSON("success");
    }

    /**
     * 审核驳回
     *
     * @param id
     * @return
     */
    @RequestMapping("/reject")
    @ResponseBody
    public Result reject(Integer id) {
        agentUserWithdrawService.reject(id);

        return this.sucessJSON("success");
    }

    @RequestMapping("/messageList")
    public String messageList(SplitPageBean splitPageBean, String username) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(username)) {
                paramap.put("username", username);
            }
            List<AgentUserMessage> list = agentUserMessageService.page(paramap, splitPageBean);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/agent/messageList");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/messageAdd")
    public String messageAdd(Integer userId) {
        request.setAttribute("userId", userId);
        return this.sucess("/web/after/agent/messageAdd");
    }

    @RequestMapping("/sendMessageAlert")
    @ResponseBody
    public Result sendMessageAlert(AgentUserMessage param) {
        try {
            AgentUser user = agentUserService.get(param.getUserId());

            param.setUserName(user.getUsername());
            param.setIsRead(0);
            param.setCreateTime(new Date());
            agentUserMessageService.add(param);
            return sucessJSON("success");
        } catch (Exception e) {
            return errorJSON("系统繁忙");
        }
    }

    @RequestMapping("/sslist")
    @ResponseBody
    public Result sslist() {
        try {

            //获取省份
            List<SsProvinces> list = appSsProvincesService.list();
            //获取市
            List<SsCities> shiList = appSsCitiesService.list();
            List<SsArea> areaList = areaService.list();
            for (SsProvinces ssProvinces : list) {
                List<SsCities> citiesList=new ArrayList<>();
                for (SsCities ssCities : shiList) {
                    if(ssCities.getProvinceid().equals(ssProvinces.getProvinceid())){
                        List<SsArea> areaList1=new ArrayList<>();
                        for (SsArea a : areaList) {
                            if(a.getParentId().equals(ssCities.getCityid())) {
                                areaList1.add(a);
                            }
                        }
                        ssCities.setAreaList(areaList1);
                        citiesList.add(ssCities);
                    }
                }
                ssProvinces.setCitiesList(citiesList);
            }
            Map<String,Object> toMap=new HashMap<>(16);
            toMap.put("list", list);
            return this.sucessJSON(toMap,"成功");
        } catch (Exception e) {
            logger.error("获取省市列表异常", e);
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }
}
